from flask import (Blueprint,
                   render_template,
                   request,
                   redirect,
                   url_for, jsonify,
                   session,
                   flash)
from exts import mail, db
from flask_mail import Message
from models import EmailCaptureModel, UserModel
import string
import random
from datetime import datetime
from .forms import RegisterForm, LoginForm, ResetPasswordForm
from werkzeug.security import generate_password_hash, check_password_hash

bp = Blueprint('user', __name__, url_prefix="/user")


@bp.route("/login", methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    else:
        form = LoginForm(request.form)
        if form.validate():
            email = form.email.data
            password = form.password.data
            user = UserModel.query.filter_by(email=email).first()
            if user and check_password_hash(user.password, password):
                session['user_id'] = user.id
                return redirect("/")
            else:
                flash("邮箱或密码不匹配！！！")
                return redirect(url_for("user.login"))
        else:
            flash("邮箱或密码格式错误！！！")
            return redirect(url_for("user.login"))


@bp.route("/reset", methods=["GET", "POST"])
def reset():
    if request.method == 'GET':
        return render_template('reset.html')
    else:
        form = ResetPasswordForm(request.form)
        if form.validate():
            email = form.email.data
            password = form.password.data
            hash_password = generate_password_hash(password)
            user = UserModel.query.filter_by(email=email).first()
            try:
                if email == user.email:
                    user.password = hash_password
                    db.session.commit()
                    return redirect(url_for('user.login'))
            except:
                flash('邮箱不存在！')
                return redirect(url_for('user.reset'))
        else:
            flash('两次输入的密码不一致，请重新输入！！！')
            return redirect(url_for('user.reset'))


@bp.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'GET':
        return render_template('register.html')
    else:
        form = RegisterForm(request.form)
        if form.validate():
            email = form.email.data
            username = form.username.data
            password = form.password.data
            hash_password = generate_password_hash(password)
            user = UserModel(email=email, username=username, password=hash_password)
            db.session.add(user)
            db.session.commit()
            return redirect(url_for("user.login"))
        else:
            return redirect(url_for("user.register"))


@bp.route("/logout")
def logout():
    session.clear()
    return redirect(url_for('user.login'))


@bp.route('/capture', methods=['POST'])
def get_capture():
    email = request.form.get('email')
    letters = string.ascii_letters + string.digits
    capture = "".join(random.sample(letters, 4))
    if email:
        message = Message(
            subject='邮箱测试',
            recipients=[email],
            body=f'【Huty摄影作品展】您的注册验证码是{capture}。请勿告诉任何人！'
        )
        mail.send(message)
        capture_model = EmailCaptureModel.query.filter_by(email=email).first()
        if capture_model:
            capture_model.capture = capture
            capture_model.create_time = datetime.now()
            db.session.commit()
        else:
            capture_model = EmailCaptureModel(email=email, capture=capture)
            db.session.add(capture_model)
            db.session.commit()
        return jsonify({"code": 200})
    else:
        return jsonify({"code": 400, "message": "请先输入邮箱"})
